import 'package:flutter/material.dart';
import '../../tokens/colors.dart';
import '../../tokens/spacing.dart';
import '../../tokens/typography.dart';
import '../../tokens/borders.dart';
import '../../tokens/shadows.dart';

/// UI组件库列表样式
/// 提供统一的列表样式配置
class UIListStyles {
  // 基础列表项装饰
  static BoxDecoration basicListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      border: Border(
        bottom: BorderSide(
          color: colorScheme.outline.withOpacity(0.1),
          width: UIBorders.widthThin,
        ),
      ),
    );
  }

  // 卡片式列表项装饰
  static BoxDecoration cardListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.md,
      boxShadow: UIShadows.cardShadow,
      border: Border.all(
        color: colorScheme.outline.withOpacity(0.1),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 选中状态列表项装饰
  static BoxDecoration selectedListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.primaryContainer.withOpacity(0.3),
      borderRadius: UIBorders.sm,
      border: Border.all(
        color: colorScheme.primary.withOpacity(0.5),
        width: UIBorders.widthMedium,
      ),
    );
  }

  // 悬停状态列表项装饰
  static BoxDecoration hoverListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surfaceVariant.withOpacity(0.3),
      borderRadius: UIBorders.sm,
    );
  }

  // 分组列表头部装饰
  static BoxDecoration listGroupHeader(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surfaceVariant.withOpacity(0.5),
      border: Border(
        bottom: BorderSide(
          color: colorScheme.outline.withOpacity(0.2),
          width: UIBorders.widthThin,
        ),
      ),
    );
  }

  // 分割线装饰
  static BoxDecoration listDivider(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.outline.withOpacity(0.1),
    );
  }

  // 列表容器装饰
  static BoxDecoration listContainer(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.lg,
      boxShadow: UIShadows.cardShadow,
    );
  }

  // 嵌套列表项装饰
  static BoxDecoration nestedListItem(ColorScheme colorScheme, {int level = 1}) {
    return BoxDecoration(
      color: colorScheme.surface,
      border: Border(
        left: BorderSide(
          color: colorScheme.primary.withOpacity(0.3),
          width: UIBorders.widthMedium * level,
        ),
        bottom: BorderSide(
          color: colorScheme.outline.withOpacity(0.1),
          width: UIBorders.widthThin,
        ),
      ),
    );
  }

  // 紧凑列表项装饰
  static BoxDecoration compactListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      border: Border(
        bottom: BorderSide(
          color: colorScheme.outline.withOpacity(0.05),
          width: UIBorders.widthThin,
        ),
      ),
    );
  }

  // 可展开列表项装饰
  static BoxDecoration expandableListItem(ColorScheme colorScheme, {bool isExpanded = false}) {
    return BoxDecoration(
      color: isExpanded 
          ? colorScheme.secondaryContainer.withOpacity(0.2)
          : colorScheme.surface,
      borderRadius: isExpanded ? UIBorders.sm : null,
      border: isExpanded 
          ? Border.all(
              color: colorScheme.secondary.withOpacity(0.3),
              width: UIBorders.widthThin,
            )
          : Border(
              bottom: BorderSide(
                color: colorScheme.outline.withOpacity(0.1),
                width: UIBorders.widthThin,
              ),
            ),
    );
  }

  // 列表标题主题
  static ListTileThemeData standardListTile(ColorScheme colorScheme) {
    return ListTileThemeData(
      tileColor: colorScheme.surface,
      selectedTileColor: colorScheme.primaryContainer.withOpacity(0.3),
      contentPadding: const EdgeInsets.symmetric(
        horizontal: UISpacing.listItemPadding,
        vertical: UISpacing.listItemSpacing,
      ),
      titleTextStyle: UITextStyles.listTitle.copyWith(
        color: colorScheme.onSurface,
      ),
      subtitleTextStyle: UITextStyles.listSubtitle.copyWith(
        color: colorScheme.onSurfaceVariant,
      ),
      leadingAndTrailingTextStyle: UITextStyles.bodyMedium.copyWith(
        color: colorScheme.onSurfaceVariant,
      ),
      iconColor: colorScheme.onSurfaceVariant,
      selectedColor: colorScheme.primary,
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.sm,
      ),
      minTileHeight: 48,
      minVerticalPadding: UISpacing.listItemSpacing,
    );
  }

  // 紧凑列表标题主题
  static ListTileThemeData compactListTile(ColorScheme colorScheme) {
    return ListTileThemeData(
      tileColor: colorScheme.surface,
      selectedTileColor: colorScheme.primaryContainer.withOpacity(0.3),
      contentPadding: const EdgeInsets.symmetric(
        horizontal: UISpacing.md,
        vertical: UISpacing.xs,
      ),
      titleTextStyle: UITextStyles.bodyMedium.copyWith(
        color: colorScheme.onSurface,
      ),
      subtitleTextStyle: UITextStyles.bodySmall.copyWith(
        color: colorScheme.onSurfaceVariant,
      ),
      leadingAndTrailingTextStyle: UITextStyles.bodySmall.copyWith(
        color: colorScheme.onSurfaceVariant,
      ),
      iconColor: colorScheme.onSurfaceVariant,
      selectedColor: colorScheme.primary,
      minTileHeight: 40,
      minVerticalPadding: UISpacing.xs,
    );
  }

  // 大尺寸列表标题主题
  static ListTileThemeData largeListTile(ColorScheme colorScheme) {
    return ListTileThemeData(
      tileColor: colorScheme.surface,
      selectedTileColor: colorScheme.primaryContainer.withOpacity(0.3),
      contentPadding: const EdgeInsets.symmetric(
        horizontal: UISpacing.listItemPadding,
        vertical: UISpacing.md,
      ),
      titleTextStyle: UITextStyles.titleMedium.copyWith(
        color: colorScheme.onSurface,
      ),
      subtitleTextStyle: UITextStyles.bodyMedium.copyWith(
        color: colorScheme.onSurfaceVariant,
      ),
      leadingAndTrailingTextStyle: UITextStyles.bodyMedium.copyWith(
        color: colorScheme.onSurfaceVariant,
      ),
      iconColor: colorScheme.onSurfaceVariant,
      selectedColor: colorScheme.primary,
      minTileHeight: 64,
      minVerticalPadding: UISpacing.md,
    );
  }

  // 列表文本样式
  static TextStyle listTitleStyle(ColorScheme colorScheme) {
    return UITextStyles.listTitle.copyWith(
      color: colorScheme.onSurface,
    );
  }

  static TextStyle listSubtitleStyle(ColorScheme colorScheme) {
    return UITextStyles.listSubtitle.copyWith(
      color: colorScheme.onSurfaceVariant,
    );
  }

  static TextStyle listCaptionStyle(ColorScheme colorScheme) {
    return UITextStyles.listCaption.copyWith(
      color: colorScheme.onSurfaceVariant.withOpacity(0.7),
    );
  }

  static TextStyle listGroupHeaderStyle(ColorScheme colorScheme) {
    return UITextStyles.titleSmall.copyWith(
      color: colorScheme.onSurfaceVariant,
      fontWeight: FontWeight.w600,
      letterSpacing: 0.5,
    );
  }

  // 列表内边距配置
  static EdgeInsets get standardPadding => const EdgeInsets.symmetric(
    horizontal: UISpacing.listItemPadding,
    vertical: UISpacing.listItemSpacing,
  );

  static EdgeInsets get compactPadding => const EdgeInsets.symmetric(
    horizontal: UISpacing.md,
    vertical: UISpacing.xs,
  );

  static EdgeInsets get largePadding => const EdgeInsets.symmetric(
    horizontal: UISpacing.listItemPadding,
    vertical: UISpacing.md,
  );

  static EdgeInsets get groupHeaderPadding => const EdgeInsets.symmetric(
    horizontal: UISpacing.listItemPadding,
    vertical: UISpacing.sm,
  );

  static EdgeInsets get nestedPadding => const EdgeInsets.only(
    left: UISpacing.xl,
    right: UISpacing.listItemPadding,
    top: UISpacing.listItemSpacing,
    bottom: UISpacing.listItemSpacing,
  );

  // 列表间距配置
  static double get itemSpacing => UISpacing.listItemSpacing;
  static double get sectionSpacing => UISpacing.listSectionSpacing;
  static double get groupSpacing => UISpacing.groupSpacing;

  // 列表项高度配置
  static const double compactItemHeight = 40.0;
  static const double standardItemHeight = 56.0;
  static const double largeItemHeight = 72.0;
  static const double headerItemHeight = 48.0;

  // 特殊状态列表项装饰
  static BoxDecoration errorListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.errorContainer.withOpacity(0.1),
      borderRadius: UIBorders.sm,
      border: Border.all(
        color: colorScheme.error.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  static BoxDecoration warningListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: UIColors.warningContainer.withOpacity(0.1),
      borderRadius: UIBorders.sm,
      border: Border.all(
        color: UIColors.warning.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  static BoxDecoration successListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: UIColors.successContainer.withOpacity(0.1),
      borderRadius: UIBorders.sm,
      border: Border.all(
        color: UIColors.success.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  static BoxDecoration infoListItem(ColorScheme colorScheme) {
    return BoxDecoration(
      color: UIColors.infoContainer.withOpacity(0.1),
      borderRadius: UIBorders.sm,
      border: Border.all(
        color: UIColors.info.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 获取列表项样式工厂方法
  static BoxDecoration getListItemStyle({
    required String type,
    required ColorScheme colorScheme,
    bool isSelected = false,
    bool isHover = false,
    bool isExpanded = false,
    int nestLevel = 0,
  }) {
    // 处理状态
    if (isSelected) {
      return selectedListItem(colorScheme);
    }
    if (isHover) {
      return hoverListItem(colorScheme);
    }

    // 处理嵌套
    if (nestLevel > 0) {
      return nestedListItem(colorScheme, level: nestLevel);
    }

    switch (type.toLowerCase()) {
      case 'card':
        return cardListItem(colorScheme);
      case 'compact':
        return compactListItem(colorScheme);
      case 'expandable':
        return expandableListItem(colorScheme, isExpanded: isExpanded);
      case 'groupheader':
        return listGroupHeader(colorScheme);
      case 'container':
        return listContainer(colorScheme);
      case 'error':
        return errorListItem(colorScheme);
      case 'warning':
        return warningListItem(colorScheme);
      case 'success':
        return successListItem(colorScheme);
      case 'info':
        return infoListItem(colorScheme);
      default:
        return basicListItem(colorScheme);
    }
  }

  // 获取列表项主题
  static ListTileThemeData getListTileTheme({
    required String size,
    required ColorScheme colorScheme,
  }) {
    switch (size.toLowerCase()) {
      case 'compact':
        return compactListTile(colorScheme);
      case 'large':
        return largeListTile(colorScheme);
      default:
        return standardListTile(colorScheme);
    }
  }

  // 获取列表项高度
  static double getListItemHeight(String size) {
    switch (size.toLowerCase()) {
      case 'compact':
        return compactItemHeight;
      case 'large':
        return largeItemHeight;
      case 'header':
        return headerItemHeight;
      default:
        return standardItemHeight;
    }
  }
}